package cn.itsource.pethome.system.permission.service.impl;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.permission.domain.Permission;
import cn.itsource.pethome.system.permission.mapper.PermissionMapper;
import cn.itsource.pethome.system.permission.service.IPermissionService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.utils.Classutil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-02-01
 */
@Service
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements IPermissionService {
    private static final String  PKG_PREFIX   = "cn.itsource.pethome.system.";
    private static final String  PKG_SUFFIX   = ".controller";
    @Resource
    private PermissionMapper permissionMapper;

    //扫描权限，添加到数据库中
    @Override
    public void scanPermission() {
        //获取根目录下面所有的模块目录
        String path = this.getClass().getResource("/").getPath() + "/cn/itsource/pethome/system/";
        File file = new File(path);
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isDirectory();//如果是文件夹，就返回true
            }
        });
        //获取cn.itsource.pethome.system.*.controller里面所有的类，添加到clazzes
        Set<Class> clazzes = new HashSet<>();
        for (File fileTmp : files) {
            clazzes.addAll(Classutil.getClasses(PKG_PREFIX+fileTmp.getName()+PKG_SUFFIX));
        }
        //遍历所有类，获取方法，注解数据，存数据库
        for (Class clazz : clazzes) {
            Method[] methods = clazz.getMethods();//得到当前类的所有方法
            if(methods == null){
                continue;//如果一个类中没有方法，结束本次循环
            }
            for (Method method : methods) {
                String uri = getUri(clazz, method);//获取对外的路径
                PreAuthorize annotation = method.getAnnotation(PreAuthorize.class);
                if(annotation == null){//没有就结束本次循环
                    continue;
                }
                //获取注解的数据
                String name = annotation.name();
                String value = annotation.value();
                //保存数据库
                Permission permission = new Permission();
                permission.setName(name);
                permission.setSn(value);
                permission.setUrl(uri);
                //防止重复添加
                Permission bySn = permissionMapper.findBySn(value);
                if (bySn == null)
                    permissionMapper.add(permission);
            }
        }
    }

    //获取handmapping映射的url
    private String getUri(Class clazz, Method method) {
        String classPath = "";
        Annotation annotation = clazz.getAnnotation(RequestMapping.class);
        if (annotation!=null){
            RequestMapping requestMapping = (RequestMapping) annotation;
            String[] values = requestMapping.value();
            if(values!=null&&values.length>0){
                classPath = values[0];
                if (!"".equals(classPath)&&!classPath.startsWith("/"))
                    classPath="/"+classPath;
            }
        }
        GetMapping getMapping =  method.getAnnotation(GetMapping.class);
        String methodPath = "";
        if (getMapping!=null){
            String[] values = getMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        PostMapping postMapping =  method.getAnnotation(PostMapping.class);
        if (postMapping!=null){
            String[] values = postMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        DeleteMapping deleteMapping =  method.getAnnotation(DeleteMapping.class);
        if (deleteMapping!=null){
            String[] values = deleteMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        PutMapping putMapping =  method.getAnnotation(PutMapping.class);
        if (putMapping!=null){
            String[] values = putMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }

        }
        PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
        if (patchMapping!=null){
            String[] values = patchMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        if (requestMapping!=null){
            String[] values = requestMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        return classPath+methodPath;
    }
}
